// pages/user/index.js
import { Token } from '../../utils/token.js';

import {My} from '../my/my-model.js';
var my=new My();

import { Agent } from '../../model/agent-model.js';

var agent  = new Agent();

import { Note } from '../findworker/note-model.js';
var note = new Note(); //实例化 首页 对象

import { User } from '../../model/user-model.js';

var user  = new User();

import { Sysmsg } from '../../model/sysmsg-model.js';

var sysmsg  = new Sysmsg();

import { Company } from '../../model/company-model.js';

var company  = new Company();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isuser:true,
  },

  toIndex:function(){

    wx.redirectTo({
      url: '/pages/index/index',
    })
},


toFindjob: function (e) {

  var that = this;

  wx.redirectTo({
    url: "/pages/findjob/index"
  })

},

toMyinvate: function (e) {

  var that = this;

  wx.redirectTo({
    url: "/pages/switchrole/index"
  })


},


toSysmsg:function(){

  wx.redirectTo({
    url: '/pages/sysmsg/index',
  })
},


toMyuser:function(){

  wx.redirectTo({
    url: '/pages/user/index',
  })
},

toMymoney:function(){
  
  wx.navigateTo({
    url: '/pages/mymoney/index',
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    wx.setNavigationBarTitle({
      title: '会员中心',
    })

    var appuser = wx.getStorageSync('userInfo');

    console.log('ffffffffffffffffff');


      var params = {};

      my.UserInit((data) => {
  
          
              that.setData({
                sendnotecount:data.sendnotecount,
                noticenotecount:data.noticenotecount,
                totallooknum:data.totallooknum,
                sysmsgcount:data.sysmsgcount,
                isuser:data.isuser,
                userinfo:data.userinfo
              
              });

              wx.hideNavigationBarLoading(); //完成停止加载
              wx.stopPullDownRefresh();
          },params);


      that.setData({
        userinfo:appuser
      });
   //  }

     that.setData({
      isuser:that.data.isuser
    });
     
 },

 toLogin: function (e) {


  var that = this;

  var ctoken = wx.getStorageSync('ctoken');

  if(ctoken)
  {



  var params = {ctoken:ctoken};

  user.checkBind((data) => {

     if(data.isbind)
     {
       /*
          var companyid = wx.getStorageSync('companyid');
          if (companyid > 0) {
            wx.navigateTo({
              url: "/pages/companycenter/index"
            })
          } else {
 
            wx.navigateTo({
              url: "/pages/companylogin/index"
            })
          }
          */
          company.checkLogin((data) => {

            if(data.error ==0)
            {
              wx.navigateTo({
                url: "/pages/companycenter/index"
              })

            }else{
              wx.navigateTo({
                url: "/pages/companylogin/index"
              })

            }
             

      
              },params)




  }else{

    wx.navigateTo({
      url: "/pages/register/index"
    })

  }

     

      },params);

   
    }else{



      wx.navigateTo({
        url: "/pages/companylogin/index"
      })
    }



},



 toMatchNote:function(){

  var that = this;

var params = {};

    note.CheckNote((data) => {

      if(data.status == 1)
      {

        wx.navigateTo({
          url: '/pages/matchjob/index',
        })


      }else if(data.status == 2){

        wx.navigateTo({
          url: '/pages/mynote/index',
        })

      }else{
    
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })

      }
        
        console.log(data);

        },params);

},









refreshNotice:function(){

    var that = this;

    var params = {};

    user.checkBind((data) => {

        if(data.isbind)
        {

        note.NoteRefresh((data) => {

            if(data.status == 1)
            {

            wx.showToast({
                title: data.msg,
                icon: 'none',
                duration: 2000
            })

            }else if(data.status == 2){

            wx.navigateTo({
                url: '/pages/mynote/index',
            })

            }else{
        
            wx.showToast({
                title: data.msg,
                icon: 'none',
                duration: 2000
            })

            }
            
            console.log(data);

            },params);
    

    }else{

    wx.navigateTo({
        url: "/pages/register/index"
    })

    }

        

        },params);

   

  },

  bindGetUserInfo:function(){

    var that=this;

    wx.navigateTo({
      url: "/pages/bindwx/index"
    })




    
  },

  updateuserinfo:function(){
    
    var that = this;
    var userinfo = wx.getStorageSync('userInfo');
    var params = {nickname:userinfo.nickName,avatarUrl:userinfo.avatarUrl};

    user.Updateuser((data) => {

        
      that.data.istel= data.istel;
      that.setData({
      
        istel:that.data.istel
     });

        },params);

  },
  toAgentcenter:function()
  {

    var that = this;

    var params = {};
 
    agent.Checkagent((data) => {

      if(data.status == 0 )
      {
        wx.navigateTo({
          url: "/pages/regagent/index"
        })

      }else if(data.status == 1){
        wx.navigateTo({
          url: "/pages/agentcenter/index"
        })
      
      }else{

        wx.showModal({
          title: '提示',
          content: data.msg,
          showCancel: false
        })
        return

      }

    },params);

   
  },
  toMydone:function()
  {
    var that = this;
    wx.navigateTo({
      url: "/pages/mydone/index"
    })

  },
  toHelp:function()
  {
    var that = this;
    wx.navigateTo({
      url: "/pages/article/index?id=846"
    })

  },

  toMyComment:function()
  {
    var that = this;
    wx.navigateTo({
      url: "/pages/mycomment/index"
    })

  },

  toMylooknote:function()
  {
    var that = this;
    wx.navigateTo({
      url: "/pages/mylooknote/index"
    })

  },
  toMyNote: function () {
    var that = this;

    var that = this;

    var params = {};



    user.checkBind((data) => {

       if(data.isbind)
       {

        wx.navigateTo({
          url: "/pages/notecenter/index"
        })
     

    }else{

      wx.navigateTo({
        url: "/pages/register/index"
      })

    }

       

        },params);
  
  },
  toMyFind:function(e){
    var that = this;
      var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/myfind/index?id="+id
    })

  },
  toLookrole:function(){

    var that = this;
    wx.navigateTo({
      url: "/pages/lookrole/index"
    })
  },
 

  toMymoneyrecord:function(){
    var that = this;
    wx.navigateTo({
      url: "/pages/mymoneyrecord/index"
    })

  },


  toMySave: function (e) {

    var that = this;

    wx.navigateTo({
      url: "/pages/mysave/index"
    })

  },
  
  toMyGz: function (e) {

    var that = this;

    wx.navigateTo({
      url: "/pages/mygz/index"
    })
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      var that = this;
      that.onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */ 
  onPullDownRefresh: function(){
    wx.showNavigationBarLoading();
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

    var that = this;
      return {
          title:'会员中心' ,
          path: '/pages/user/index'
      }

  }
})